package com.ddatsh.transaction.core.model;


import java.util.concurrent.PriorityBlockingQueue;

/**
 * 请求队列，当请求并发数超过系统容量设计，将超出部分进入该队列
 * 
 * @author ddatsh
 * @version 1.0
 */
public class CapacityExtQueue {
	private PriorityBlockingQueue<RequestPack> requestQueue;

	/**
	 * Creates a new CapacityExtQueue object. 构造方法
	 * 
	 * @param requestQueueSize
	 *            DOCUMENT ME!
	 */
	public CapacityExtQueue(int requestQueueSize) {
		super();
		this.requestQueue = new PriorityBlockingQueue<RequestPack>(requestQueueSize);
	}

	/**
	 * 将请求加入队列
	 * 
	 * @param newRequest
	 *            请求对象
	 */
	public void addRequest(RequestPack newRequest) {
		requestQueue.add(newRequest);
	}

	/**
	 * 请求对象
	 * 
	 * @return 获取队列头请求包
	 */
	public RequestPack pollRequest() {
		return requestQueue.poll();
	}

	/**
	 * 请求队列计数
	 * 
	 * @return 请求队列数量
	 */
	public int count() {
		return requestQueue.size();
	}

	/**
	 * 内部包装类，包装请求线程和请求对象到pack中
	 * 
	 * @author dongfeng.zhang
	 * 
	 */
	public class RequestPack {
		Request requestObject;
		Thread requestThread;

		private RequestPack(Request requestObject, Thread requestThread) {
			super();
			this.requestObject = requestObject;
			this.requestThread = requestThread;
		}

		public Request getRequestObject() {
			return requestObject;
		}

		public Thread getRequestThread() {
			return requestThread;
		}

		public void setRequestObject(Request requestObject) {
			this.requestObject = requestObject;
		}

		public void setRequestThread(Thread requestThread) {
			this.requestThread = requestThread;
		}
	}
}
